<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true">
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="风险类型" prop="riskType">
        <el-select
          v-model="queryParams.riskType"
          placeholder="请选择风险类型"
          clearable
          class="!w-240px"
        >
          <el-option :label="value" :value="key" v-for="(value,key) in riskTypeDict" :key="key"/>
        </el-select>
      </el-form-item>
      <el-form-item label="风险对象" prop="riskTarget">
        <el-input
          v-model="queryParams.riskTarget"
          placeholder="请输入风险对象"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="编码" prop="identifyContentCode">
        <el-input
          v-model="queryParams.identifyContentCode"
          placeholder="请输入编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option :label="value" :value="key" v-for="(value,key) in statusDict" :key="key"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px"/>
          搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px"/>
          重置
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
        >
          <Icon icon="ep:plus" class="mr-5px"/>
          新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['zjyj:risk-identify-object:export']"
        >
          <Icon icon="ep:download" class="mr-5px"/>
          导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading" :data="list" row-key="id" :stripe="true"
      :show-overflow-tooltip="true"
      :default-expand-all="true"
    >
      <!--      <el-table-column type="expand">
              <template #default="props">
                &lt;!&ndash; 子表格定义 &ndash;&gt;
                <el-table
                  :data="props.row.versions"
                  class="pl-10"
                  style="width: 100%;">
                  &lt;!&ndash; 定义子表格的列 &ndash;&gt;
                  <el-table-column label="辨识内容" align="center" prop="identifyContent"/>
                  <el-table-column label="辨识内容编码" align="center" prop="identifyContentCode"/>
                  <el-table-column prop="hazard" label="危害"/>
                  <el-table-column prop="projectSolution" label="工程解决方案"/>
                  <el-table-column prop="managerSolution" label="管理解决方案"/>
                  <el-table-column prop="selfSolution" label="个人解决方案"/>
                  <el-table-column prop="trainEduSolution" label="培训教育解决方案"/>
                  <el-table-column prop="badSolution" label="风险等级"/>
                  <el-table-column prop="status" label="状态" :formatter=" _row =>'未评估'"/>
                  <el-table-column label="版本操作" align="center" min-width="120px">

                    <template #default="scope">
                      <el-button
                        link
                        type="primary"
                        @click="openVersionForm(scope.row.identifyObjectId,scope.row.id)"
                      >
                        编辑
                      </el-button>
                      <el-button
                        link
                        type="danger"
                        @click="handleDelete4Version(scope.row.id)"
                      >
                        删除
                      </el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </template>
            </el-table-column>-->
      <el-table-column type="expand">
        <template #default="props">
          <el-table
            :data="props.row.instances"
            class="pl-10"
            style="width: 100%;"
            :header-cell-style="{background: '#F4F4F5'}"
          >
            <el-table-column label="设备编码" align="center" prop="instance.code"/>
            <el-table-column label="评估状态" align="center" prop="status">
              <template #default="scope">
                <el-tag :type="statusTagDict[scope.row.status]">
                  {{ riskIdentifyInstanceStatusDict[scope.row.status] }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column
              label="添加时间"
              align="center"
              prop="createTime"
              :formatter="dateFormatter"
            />
            <el-table-column label="操作" align="center" width="120">
              <template #default="scope">
                <el-button
                  link
                  type="danger"
                  @click="handleDeleteChild(props.row.id, scope.row.deviceCode)"
                >
                  编辑
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="风险点" align="center" prop="riskInfoName"/>
      <el-table-column
        label="风险类型" align="center" prop="riskType"
        :formatter="row =>  riskTypeDict[row.riskType]"/>
      <el-table-column label="风险对象数量" align="center" prop="identifyObjectSize"/>
      <el-table-column
        label="创建人" align="center" prop="creatorName"
        :formatter="row => row.creatorName || '管理员'"/>
      <el-table-column
        label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="statusTagDict[scope.row.status]">
            {{ riskIdentifyStatusDict[scope.row.status] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('create', scope.row.id)"
          >
            新增
          </el-button>
          <el-button
            link
            type="primary"
            @click="applyAssessment(scope.row.id,scope.row.status)"
          >
            申请评估
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <RiskIdentifyObjectForm ref="formRef" @success="getList"/>
</template>

<script setup lang="ts">
import {dateFormatter} from '@/utils/formatTime';
import RiskIdentifyObjectForm from './RiskIdentifyObjectForm.vue';
import {
  riskIdentifyInstanceStatusDict,
  riskIdentifyStatusDict,
  statusDict,
  statusTagDict
} from '@/views/zjyj/riskidentify/dict';
import {RiskIdentifyApi} from '@/api/zjyj/riskidentify';
import {riskTypeDict} from '@/utils/dict';

/** 风险辨识 列表 */
defineOptions({name: 'RiskIdentify'});

const message = useMessage(); // 消息弹窗
const {t} = useI18n(); // 国际化


const loading = ref(true); // 列表的加载中
const list = ref([]); // 列表的数据
const total = ref(0); // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  riskType: undefined,
  riskTypeInfoId: undefined,
  riskTarget: undefined,
  identifyContent: undefined,
  identifyContentCode: undefined,
  status: undefined,
  lastVersionNumber: undefined
});
const queryFormRef = ref(); // 搜索的表单
const exportLoading = ref(false); // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  try {
    const data = await RiskIdentifyApi.getRiskIdentifyPage(queryParams);
    list.value = data.list;
    total.value = data.total;
    // list.value = [{
    //   id: 1,
    //   riskInfoName: '郑州锅炉',
    //   riskType: 2,
    //   identifyObjectSize: 2,
    //   creatorName: '张三',
    //   status: 0,
    //   createTime: '2024-03-01 10:00:00',
    //   children: [{
    //     deviceCode: 'DEV-001',
    //     status: 0,
    //     createTime: '2024-03-01 11:00:00'
    //   }, {
    //     deviceCode: 'DEV-002',
    //     status: 1,
    //     createTime: '2024-03-01 12:00:00'
    //   }]
    // },{
    //   id: 2,
    //   riskInfoName: '叉车',
    //   riskType: 2,
    //   identifyObjectSize: 2,
    //   creatorName: '张三',
    //   status: 0,
    //   createTime: '2024-03-01 10:00:00',
    //   children: [{
    //     deviceCode: 'DEV-001',
    //     status: 0,
    //     createTime: '2024-03-01 11:00:00'
    //   }]
    // }];
    // total.value = 1;

  } finally {
    loading.value = false;
  }
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields();
  handleQuery();
};

/** 添加/修改操作 */
const formRef = ref();
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id);
};

const applyAssessment = async (id: number, status: number) => {
  // 如果状态是1，需要二次确认，提醒他已经评估过了，是否继续评估
  if (status == 1) {
    await message.confirm(`正在评估中，是否继续评估`);
  }
  const data = await RiskIdentifyApi.applyAssessment(id);
  if (data) {
    message.success('申请评估成功，请等待评估');
  }
  await getList();
};
/** 导出按钮操作 */



/** 初始化 **/
onMounted(() => {
  getList();
});
</script>
